package com.salton123.charging.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.salton123.charging.bean.LocalSoundItem;

import java.util.ArrayList;
import java.util.List;

public class DBUtils {
    private static final String TABLE_NAME = "list";
    private static MyDBHelper mHelper;

    private static class MyDBHelper extends SQLiteOpenHelper {
        public void onUpgrade(SQLiteDatabase sQLiteDatabase, int i, int i2) {
        }

        public MyDBHelper(Context context) {
            super(context, "sound_list", null, 1);
        }

        public void onCreate(SQLiteDatabase sQLiteDatabase) {
            sQLiteDatabase.execSQL(
                    "create table list(id INTEGER PRIMARY KEY AUTOINCREMENT, name text, uri text, is_res_id int(2))");
        }
    }

    public static void init(Context context) {
        if (mHelper == null) {
            mHelper = new MyDBHelper(context);
        }
    }

    public static int getCount() {
        MyDBHelper myDBHelper = mHelper;
        int i = 0;
        if (myDBHelper == null) {
            return 0;
        }
        Cursor query = myDBHelper.getReadableDatabase().query(TABLE_NAME, null, null, null, null, null, null);
        if (query != null) {
            i = query.getCount();
            query.close();
        }
        return i;
    }

    public static String getUriById(int i) {
        MyDBHelper myDBHelper = mHelper;
        if (myDBHelper == null) {
            return "";
        }
        SQLiteDatabase readableDatabase = myDBHelper.getReadableDatabase();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT uri FROM list WHERE id=");
        stringBuilder.append(i);
        String stringBuilder2 = stringBuilder.toString();
        String str = null;
        Cursor rawQuery = readableDatabase.rawQuery(stringBuilder2, null);
        if (rawQuery != null) {
            if (rawQuery.moveToNext()) {
                str = rawQuery.getString(rawQuery.getColumnIndex("uri"));
            }
            rawQuery.close();
        }
        return str;
    }

    public static LocalSoundItem getItemById(int i) {
        MyDBHelper myDBHelper = mHelper;
        LocalSoundItem localSoundItem = null;
        if (myDBHelper == null) {
            return null;
        }
        SQLiteDatabase readableDatabase = myDBHelper.getReadableDatabase();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT * FROM list WHERE id=");
        stringBuilder.append(i);
        Cursor rawQuery = readableDatabase.rawQuery(stringBuilder.toString(), null);
        if (rawQuery != null) {
            if (rawQuery.moveToNext()) {
                int i2 = rawQuery.getInt(rawQuery.getColumnIndex("id"));
                String string = rawQuery.getString(rawQuery.getColumnIndex("name"));
                String string2 = rawQuery.getString(rawQuery.getColumnIndex("uri"));
                boolean z = true;
                if (rawQuery.getInt(rawQuery.getColumnIndex("is_res_id")) != 1) {
                    z = false;
                }
                localSoundItem = new LocalSoundItem(i2, string, string2, z);
            }
            rawQuery.close();
        }
        return localSoundItem;
    }

    public static String getNameById(int i) {
        MyDBHelper myDBHelper = mHelper;
        if (myDBHelper == null) {
            return "";
        }
        SQLiteDatabase readableDatabase = myDBHelper.getReadableDatabase();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("SELECT name FROM list WHERE id=");
        stringBuilder.append(i);
        String stringBuilder2 = stringBuilder.toString();
        String str = null;
        Cursor rawQuery = readableDatabase.rawQuery(stringBuilder2, null);
        if (rawQuery != null) {
            if (rawQuery.moveToNext()) {
                str = rawQuery.getString(rawQuery.getColumnIndex("name"));
            }
            rawQuery.close();
        }
        return str;
    }

    public static int deleteById(int i) {
        MyDBHelper myDBHelper = mHelper;
        if (myDBHelper == null) {
            return -1;
        }
        SQLiteDatabase writableDatabase = myDBHelper.getWritableDatabase();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("id=");
        stringBuilder.append(i);
        return writableDatabase.delete(TABLE_NAME, stringBuilder.toString(), null);
    }

    public static int save(String name, String uri, boolean is_res_id) {
        int i = 1;
        if (mHelper == null) {
            return 1;
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        contentValues.put("uri", uri);
        contentValues.put("is_res_id", is_res_id);
        if (mHelper.getWritableDatabase().insert(TABLE_NAME, null, contentValues) <= 0) {
            return -1;
        }
        Cursor query = mHelper.getReadableDatabase().query(TABLE_NAME, null, null, null, null, null, null);
        if (query != null) {
            if (query.moveToLast()) {
                i = query.getInt(query.getColumnIndex("id"));
            }
            query.close();
        }
        return i;
    }

    public static List<LocalSoundItem> readAll() {
        ArrayList arrayList = new ArrayList();
        MyDBHelper myDBHelper = mHelper;
        if (myDBHelper == null) {
            return arrayList;
        }
        Cursor query = myDBHelper.getReadableDatabase().query(TABLE_NAME, null, null, null, null, null, null);
        if (query != null) {
            while (query.moveToNext()) {
                int i = query.getInt(query.getColumnIndex("id"));
                String string = query.getString(query.getColumnIndex("name"));
                String string2 = query.getString(query.getColumnIndex("uri"));
                boolean z = true;
                if (query.getInt(query.getColumnIndex("is_res_id")) != 1) {
                    z = false;
                }
                arrayList.add(new LocalSoundItem(i, string, string2, z));
            }
            query.close();
        }
        return arrayList;
    }
}
